package com.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.*;
import com.jl.model.*;
import com.jl.model.in.DevicePartIn;
import com.jl.service.IDevicePartService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 设备的部位列表信息 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-19
 */
@Service
public class DevicePartServiceImpl extends ServiceImpl<DevicePartMapper, DevicePart> implements IDevicePartService {

    @Override
    public void selectDevicePartDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<DevicePart> qw) {
        Page<Map<String, Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String, Object>> mapList = baseMapper.selectDataGrid(page, qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }


    @Override
    public void selectDevicePartDataGridByIds(PageDataInfo<Map<String, Object>> pageDataInfo, List<Long> ids) {
        Page<Map<String, Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String, Object>> mapList = baseMapper.selectDevicePartDataGridByIds(page, ids);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }


    @Override
    public boolean isDevicePartUsed(String name) {
        return baseMapper.selectDevicePartCount(name) > 0;
    }

    @Override
    public boolean isDevicePartCanUse(String name, Long partId) {
        DevicePartIn devicePartIn = baseMapper.selectDevicePartInByName(name);
        if (devicePartIn == null) {
            //如果找不到数据则表示该数据没有被使用
            return true;
        } else {
            if (devicePartIn.getId().equals(partId)) {
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }


    @Override
    public void selectTopFaultCountDevicePart(PageDataInfo<Map<String, Object>> pageDataInfo, Integer top, String startTime, String endTime) {
        List<Map<String, Object>> mapList = baseMapper.selectTopFaultCountDevicePart(top, startTime, endTime);
        pageDataInfo.setTotal(mapList.size());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public void selectTopChangeComponentDevicePart(PageDataInfo<Map<String, Object>> pageDataInfo, Integer top, String startTime, String endTime) {
        List<Map<String, Object>> mapList = baseMapper.selectTopChangeComponentDevicePart(top, startTime, endTime);
        pageDataInfo.setTotal(mapList.size());
        pageDataInfo.setRecords(mapList);
    }
}
